#include <iostream>
#include <vector>
#include <map>


using namespace std;
const int N = 1e3;
typedef pair<int, int> PII;

int arr[N];
map<int, int> M;

int main() {
	int n, k;

	cin >> n >> k;
	for (int i = 1; i <= n; ++ i) {
		int num;
		cin >> num;
		M[num] ++;
	}
	for (int i = 1; i <= k; ++ i) {
		int a, b;
		cin >> a >> b;
		for (auto j : M) {
			int fi = j.first;
			if (fi % 10 == a || fi / 100 == a || 
				fi / 10 % 10 == a || fi / 100 == b || 
				fi % 10 == b || fi / 10 % 10 == b)
				arr[j.first] += j.second;
		}
	}
	int max_[5] = {0};
	for (auto i : M) {
		for (int j = 0; j < 3; ++ j) {
			if (arr[i.first] >= arr[max_[j]]) {
				max_[j + 2] = max_[j + 1], max_[j + 1] = max_[j], max_[j] = i.first;
				break;
			}
		}
	}
	printf("%03d %03d %03d", max_[0], max_[1], max_[2]);
	return 0;
}
